home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / GDEVDJET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  12.3 KB  |  389 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevdjet.c */
  21. /* HP LaserJet/DeskJet driver for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gdevpcl.h"
  24.  
  25. /* Thanks to Jim Mayer, Xerox Webster Research Center, */
  26. /* and to Jan-Mark Wams (jms@cs.vu.nl) and Frans van Hoesel */
  27. /* (hoesel@rugr86.rug.nl) for improvements. */
  28.  
  29. /*
  30.  * You may select a resolution of 75, 100, 150, or 300 DPI.
  31.  * Normally you would do this in the makefile or on the gs command line,
  32.  * not here.
  33.  *
  34.  * If the preprocessor symbol A4 is defined, the default paper size is
  35.  * the European A4 size; otherwise it is the U.S. letter size (8.5"x11").
  36.  */
  37. /*#define X_DPI 300*/
  38. /*#define Y_DPI 300*/
  39.  
  40. #define X_DPI_MAX 300
  41. #define Y_DPI_MAX 300
  42.  
  43. #ifndef X_DPI
  44. #  define X_DPI X_DPI_MAX
  45. #endif
  46. #ifndef Y_DPI
  47. #  define Y_DPI Y_DPI_MAX
  48. #endif
  49.  
  50. /*
  51.  * For all DeskJet Printers:
  52.  *
  53.  *  Maximum printing width               = 2400 dots = 8"
  54.  *  All Deskjets have 1/2" unprintable bottom margin
  55.  *
  56.  * Note that the margins defined just below here apply only to the DeskJet;
  57.  * the paper size, width and height apply to the LaserJet as well.
  58.  */
  59.  
  60. /* Margins are left, bottom, right, top. */
  61. #define DESKJET_MARGINS_LETTER    0.25, 0.40, 0.25, 0.40
  62. #define DESKJET_MARGINS_A4    0.15, 0.40, 0.15, 0.05
  63.  
  64. #ifndef A4
  65. #  define WIDTH_10THS        85
  66. #  define HEIGHT_10THS        110
  67. #  define DESKJET_MARGINS    DESKJET_MARGINS_LETTER
  68. #else
  69. #  define WIDTH_10THS        83    /* 210mm */
  70. #  define HEIGHT_10THS        117    /* 297mm */
  71. #  define DESKJET_MARGINS    DESKJET_MARGINS_A4
  72. #endif
  73.  
  74. /* The number of blank lines that make it worthwhile to reposition */
  75. /* the cursor. */
  76. #define MIN_SKIP_LINES 7
  77.  
  78. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  79. #define W sizeof(word)
  80. #define LINE_SIZE ((X_DPI_MAX * 85 / 10 + W * 8 - 1) / (W * 8) * W)
  81.  
  82. /* Printer types */
  83. #define LJ    0
  84. #define LJplus    1
  85. #define LJ2p    2
  86. #define LJ3    3
  87. #define DJ    4
  88. #define DJ500    5
  89.  
  90. /* The printer initialization strings. */
  91. private const char *init_strings[] = {
  92.     /* LaserJet PCL 3, no compression */
  93.         "\033*p0x0Y\033*b0M",
  94.     /* LaserJet Plus PCL 3, no compression */
  95.         "\033*p0x0Y\033*b0M",
  96.     /* LaserJet IIP PCL 4, mode 2 compression */
  97.         "\033*r0F\033*p0x75Y\033*b2M",
  98.     /* LaserJet III PCL 5, mode 2&3 compression */
  99.         "\033*r0F\033*p0x75Y",
  100.     /* DeskJet almost PCL 4, mode 2 compression */
  101.         "\033&k1W\033*p0x0Y\033*b2M",
  102.     /* DeskJet 500 almost PCL 4, mode 2&3 compression */
  103.         "\033&k1W\033*p0x0Y",
  104. };
  105.  
  106. /* The device descriptors */
  107. private dev_proc_open_device(hpjet_open);
  108. private dev_proc_print_page(djet_print_page);
  109. private dev_proc_print_page(djet500_print_page);
  110. private dev_proc_print_page(ljet_print_page);
  111. private dev_proc_print_page(ljetplus_print_page);
  112. private dev_proc_print_page(ljet2p_print_page);
  113. private dev_proc_print_page(ljet3_print_page);
  114.  
  115. gx_device_procs prn_hp_procs =
  116.   prn_matrix_procs(hpjet_open, gdev_pcl_get_initial_matrix,
  117.     gdev_prn_output_page, gdev_prn_close);
  118.  
  119. gx_device_printer gs_deskjet_device =
  120.   prn_device(prn_hp_procs, "deskjet",
  121.     WIDTH_10THS, HEIGHT_10THS,
  122.     X_DPI, Y_DPI,
  123.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  124.     1, djet_print_page);
  125.  
  126. gx_device_printer gs_djet500_device =
  127.   prn_device(prn_hp_procs, "djet500",
  128.     WIDTH_10THS, HEIGHT_10THS,
  129.     X_DPI, Y_DPI,
  130.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  131.     1, djet500_print_page);
  132.  
  133. gx_device_printer gs_laserjet_device =
  134.   prn_device(prn_hp_procs, "laserjet",
  135.     WIDTH_10THS, HEIGHT_10THS,
  136.     X_DPI, Y_DPI,
  137.     0.05, 0.25, 0.55, 0.25,        /* margins */
  138.     1, ljet_print_page);
  139.  
  140. gx_device_printer gs_ljetplus_device =
  141.   prn_device(prn_hp_procs, "ljetplus",
  142.     WIDTH_10THS, HEIGHT_10THS,
  143.     X_DPI, Y_DPI,
  144.     0.05, 0.25, 0.55, 0.25,        /* margins */
  145.     1, ljetplus_print_page);
  146.  
  147. gx_device_printer gs_ljet2p_device =
  148.   prn_device(prn_hp_procs, "ljet2p",
  149.     WIDTH_10THS, HEIGHT_10THS,
  150.     X_DPI, Y_DPI,
  151.     0.20, 0.25, 0.25, 0.25,        /* margins */
  152.     1, ljet2p_print_page);
  153.  
  154. gx_device_printer gs_ljet3_device =
  155.   prn_device(prn_hp_procs, "ljet3",
  156.     WIDTH_10THS, HEIGHT_10THS,
  157.     X_DPI, Y_DPI,
  158.     0.20, 0.25, 0.25, 0.25,        /* margins */
  159.     1, ljet3_print_page);
  160.  
  161. /* Forward references */
  162. private int hpjet_print_page(P3(gx_device_printer *, FILE *, int));
  163.  
  164. /* Open the printer, adjusting the margins if necessary. */
  165. private int
  166. hpjet_open(gx_device *pdev)
  167. {    /* Change the margins if necessary. */
  168. #define ppdev ((gx_device_printer *)pdev)
  169.     if ( ppdev->print_page == djet_print_page ||
  170.          ppdev->print_page == djet500_print_page
  171.        )
  172. #undef ppdev
  173.     {    static const float m_a4[4] = { DESKJET_MARGINS_A4 };
  174.         static const float m_letter[4] = { DESKJET_MARGINS_LETTER };
  175.         const float _ds *m =
  176.             (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  177.              m_letter);
  178.         pdev->l_margin = m[0];
  179.         pdev->b_margin = m[1];
  180.         pdev->r_margin = m[2];
  181.         pdev->t_margin = m[3];
  182.     }
  183.     return gdev_prn_open(pdev);
  184. }
  185.  
  186. /* ------ Internal routines ------ */
  187.  
  188. /* The DeskJet can compress (mode 2) */
  189. private int
  190. djet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  191. {    return hpjet_print_page(pdev, prn_stream, DJ);
  192. }
  193. /* The DeskJet500 can compress (mode 3) */
  194. private int
  195. djet500_print_page(gx_device_printer *pdev, FILE *prn_stream)
  196. {    return hpjet_print_page(pdev, prn_stream, DJ500);
  197. }
  198. /* The LaserJet series II can't compress */
  199. private int
  200. ljet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  201. {    return hpjet_print_page(pdev, prn_stream, LJ);
  202. }
  203. /* The LaserJet Plus can't compress */
  204. private int
  205. ljetplus_print_page(gx_device_printer *pdev, FILE *prn_stream)
  206. {    return hpjet_print_page(pdev, prn_stream, LJplus);
  207. }
  208. /* All LaserJet series IIIs (III,IIId,IIIp,IIIsi) compress (mode 3) */
  209. private int
  210. ljet3_print_page(gx_device_printer *pdev, FILE *prn_stream)
  211. {    return hpjet_print_page(pdev, prn_stream, LJ3);
  212. }
  213. /* LaserJet series IIp & IId compress (mode 2) */
  214. private int
  215. ljet2p_print_page(gx_device_printer *pdev, FILE *prn_stream)
  216. {    return hpjet_print_page(pdev, prn_stream, LJ2p);
  217. }
  218.  
  219. /* Send the page to the printer.  For speed, compress each scan line, */
  220. /* since computer-to-printer communication time is often a bottleneck. */
  221. private int
  222. hpjet_print_page(gx_device_printer *pdev, FILE *prn_stream, int ptype)
  223. {    int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  224.     int line_size_words = (line_size + W - 1) / W;
  225.     uint storage_size_words = line_size_words * 8; /* data, out_row, out_row_alt, prev_row */
  226.     word *storage = (ulong *)gs_malloc(storage_size_words, W,
  227.                        "hpjet_print_page");
  228.     word
  229.       *data_words,
  230.       *out_row_words,
  231.       *out_row_alt_words,
  232.       *prev_row_words;
  233. #define data ((char *)data_words)
  234. #define out_row ((char *)out_row_words)
  235. #define out_row_alt ((char *)out_row_alt_words)
  236. #define prev_row ((char *)prev_row_words)
  237.     char *out_data;
  238.     int x_dpi = pdev->x_pixels_per_inch;
  239.     int y_dots_per_pixel = Y_DPI_MAX / pdev->y_pixels_per_inch;
  240.     int out_count;
  241.     int compression = -1;
  242.     static const char from2to3[] = "\033*rB\033*r1A\033*b3m";
  243.     int penalty_from2to3 = strlen(from2to3);
  244.     static const char from3to2[] = "\033*rB\033*r1A\033*b2m";
  245.     int penalty_from3to2 = strlen(from3to2);
  246.     int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  247.  
  248.     if ( storage == 0 )    /* can't allocate working area */
  249.         return_error(gs_error_VMerror);
  250.     data_words = storage;
  251.     out_row_words = data_words + (line_size_words * 2);
  252.     out_row_alt_words = out_row_words + (line_size_words * 2);
  253.     prev_row_words = out_row_alt_words + (line_size_words * 2);
  254.     /* Clear temp storage */
  255.     memset(data, 0, storage_size_words * W);
  256.  
  257.     /* Initialize printer. */
  258.     fputs("\033E", prn_stream);        /* reset printer */
  259.     fputs("\033*rB", prn_stream);        /* end raster graphics */
  260.     fprintf(prn_stream, "\033*t%dR", x_dpi);    /* set resolution */
  261.     /* If the printer supports it, set the paper size */
  262.     /* based on the actual requested size. */
  263.     if ( !(ptype == LJ || ptype == LJplus) )
  264.     {    fprintf(prn_stream, "\033&l%dA", paper_size);
  265.     }
  266.     fputs("\033&l0o0e0L", prn_stream);
  267.     fputs(init_strings[ptype], prn_stream);
  268.  
  269.     /* Send each scan line in turn */
  270.        {    int lnum;
  271.         int num_blank_lines = 0;
  272.         word rmask = ~(word)0 << (-pdev->width & (W * 8 - 1));
  273.  
  274.         /* Transfer raster graphics. */
  275.         for ( lnum = 0; lnum < pdev->height; lnum++ )
  276.            {    register word *end_data = data_words + line_size_words;
  277.             gdev_prn_copy_scan_lines(pdev, lnum,
  278.                          (byte *)data, line_size);
  279.                /* Mask off 1-bits beyond the line width. */
  280.             end_data[-1] &= rmask;
  281.             /* Remove trailing 0s. */
  282.             while ( end_data > data_words && end_data[-1] == 0 )
  283.               end_data--;
  284.             if ( end_data == data_words )
  285.                {    /* Blank line */
  286.                 num_blank_lines++;
  287.                 continue;
  288.                }
  289.  
  290.             /* We've reached a non-blank line. */
  291.             /* Put out a spacing command if necessary. */
  292.             if ( num_blank_lines == lnum )
  293.             {    /* We're at the top of a page. */
  294.                 if ( num_blank_lines > 0 )
  295.                 {    /* move down from current position */
  296.                     fprintf(prn_stream, "\033*p+%dY",
  297.                         num_blank_lines * y_dots_per_pixel);
  298.                 }
  299.                 /* Start raster graphics. */
  300.                 fputs("\033*r1A", prn_stream);
  301.             }
  302.             /* Skip blank lines if any */
  303.             else if ( num_blank_lines != 0 )
  304.             {  /* If we can do mode 3 compression, */
  305.                /* clear our record of the seed row. */
  306.                memset(prev_row, 0, line_size);
  307.                if ( num_blank_lines < MIN_SKIP_LINES )
  308.                {    /* Moving down from current position */
  309.                 /* causes head motion on the DeskJet, so */
  310.                 /* if the number of lines is small, */
  311.                 /* we're better off printing blanks. */
  312.                 /* Make sure we clear the seed row. */
  313.                 fputs("\033*by0", prn_stream);
  314.                 for ( ; num_blank_lines > 1; num_blank_lines-- )
  315.                     fputs("w", prn_stream);
  316.                 fputs("W", prn_stream);
  317.                }
  318.                else
  319.                {    fprintf(prn_stream, "\033*b%dY", num_blank_lines);
  320.                }
  321.             }
  322.             num_blank_lines = 0;
  323.  
  324.             /* Choose the best compression mode */
  325.             /* for this particular line. */
  326.             switch (ptype)
  327.               {
  328.               case LJ3:
  329.               case DJ500:
  330.                {    /* Compression modes 2 and 3 are both */
  331.                 /* available.  Try both and see which one */
  332.                 /* produces the least output data. */
  333.                 int count3 = gdev_pcl_mode3compress(line_size, data,
  334.                                prev_row, out_row);
  335.                 int count2 = gdev_pcl_mode2compress(data_words, end_data,
  336.                                out_row_alt);
  337.                 /* The switching penalty is quite large, */
  338.                 /* because PCL demands that we end raster */
  339.                 /* graphics and start it again.... */
  340.                 int penalty3 =
  341.                   (compression == 3 ? 0 : penalty_from2to3);
  342.                 int penalty2 =
  343.                   (compression == 2 ? 0 : penalty_from3to2);
  344.                 if ( count3 + penalty3 < count2 + penalty2)
  345.                    {    if ( compression != 3 )
  346.                         fputs(from2to3, prn_stream);
  347.                     compression = 3;
  348.                     out_data = out_row;
  349.                     out_count = count3;
  350.                    }
  351.                 else
  352.                    {    if ( compression != 2 )
  353.                         fputs(from3to2, prn_stream);
  354.                     compression = 2;
  355.                     out_data = out_row_alt;
  356.                     out_count = count2;
  357.                    }
  358.                 break;
  359.                }
  360.               case DJ:
  361.               case LJ2p:
  362.                 out_data = out_row;
  363.                    out_count = gdev_pcl_mode2compress(data_words, end_data,
  364.                               out_row);
  365.                 break;
  366.               default:
  367.                 out_data = data;
  368.                 out_count = (char *)end_data - data;
  369.               }
  370.  
  371.             /* Transfer the data */
  372.             fprintf(prn_stream, "\033*b%dW", out_count);
  373.             fwrite(out_data, sizeof(char), out_count,
  374.                    prn_stream);
  375.            }
  376.     }
  377.  
  378.     /* end raster graphics */
  379.     fputs("\033*rB", prn_stream);
  380.  
  381.     /* eject page */
  382.     fputs("\033&l0H", prn_stream);
  383.  
  384.     /* free temporary storage */
  385.     gs_free((char *)storage, storage_size_words, W, "hpjet_print_page");
  386.  
  387.     return 0;
  388. }
  389.